package com.kedao.controller;

import java.util.List;

import com.jfinal.core.Controller;
import com.kedao.model.User;
import com.kedao.util.DateUtils;
import com.kedao.util.safe.MD5;

/**
 * 
 * @author vimesly
 * @Before(DemoInterceptor.class) // 配置一个Controller级别的拦截器
 */
public class IndexController extends Controller {

	/**
	 * @Before(AaaInterceptor.class) "配置一个action级别的拦截器"
	 * @Before(EvictInterceptor.class) //销毁缓存
	 * @Before(CacheInterceptor.class) // 缓存数据
	 * @CacheName("userList")
	 */

	public void index() {
		List<User> userList = new User().find("select * from kd_user");
		setAttr("userList", userList);
		render("/views/index.html");
	}

	public void find() {
		Integer id = getParaToInt("id");
		User aUser = new User().findById(id);
		setAttr("aUser", aUser);
		render("/views/find.html");
	}

	public void edit() {
		Integer id = getParaToInt("id");
		String username = getPara("username");
		String password = getPara("password");
		Integer status = getParaToInt("status");
		
		User aUser = new User().findById(id);
		if(aUser!=null) {
			aUser.set("username", username).set("password", password)
			.set("status", status).update();
		}else {
			new User().set("username", username).set("password", password)
			.set("status", status).save();
		}
		redirect("/");
	}

	public void del() {
		Integer id = getParaToInt("id");
		boolean b = new User().findById(id).delete();
		System.out.println(b);
		redirect("/");
	}

}
